//
//  OrderDetailCell.swift
//  globalcarbusiness
//
//  Created by dongzhenyu on 2017/8/30.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit

class OrderDetailCell: UITableViewCell, Reusable {

    
    var orderDetailModel: OrderDetailModel? {
        didSet {
            
            guard let nameText = orderDetailModel?.contact else {
                return
            }
            nameLabel.text = orderDetailModel?.contact
            let nameWidth: CGFloat = StringSize(nameText as NSString, fontSize: 15*HEIGHT_SCALE).width + 5 > kScreenW - 40*WIDTH_SCALE ? kScreenW - 40*WIDTH_SCALE : StringSize(nameText as NSString, fontSize: 15*HEIGHT_SCALE).width + 5
            nameLabel.snp.updateConstraints { (make) in
                make.size.equalTo(CGSize(width: nameWidth, height: 16*HEIGHT_SCALE))
            }
            nameLabel.sizeToFit()

            guard let textType = orderDetailModel?.type else {
                return
            }
            serviceType.text = textType
            serviceType.snp.updateConstraints { (make) in
                make.size.equalTo(CGSize(width: StringSize(textType as NSString, fontSize: 15*HEIGHT_SCALE).width + 5, height: 16*HEIGHT_SCALE))
            }
            
            guard let status = orderDetailModel?.order_status else {
                return
            }
            appointLabel.isHidden = Int(status) == 0 ? false : true
            
            switch textType {
            case "洗车":
                typeImageView.image = UIImage(named: "xi")
            case "到店保养":
                typeImageView.image = UIImage(named: "dian")
            case "上门保养":
                typeImageView.image = UIImage(named: "men")
            case "闪付":
                typeImageView.image = UIImage(named: "shan")
            case "到店保养4S":
                typeImageView.image = UIImage(named: "dian")
            case "钣喷":
                typeImageView.image = UIImage(named: "banpen_14")
            case "美容":
                typeImageView.image = UIImage(named: "mei")
            default:
                typeImageView.image = UIImage(named: "ta")
//                break
            }
          
            guard let price = orderDetailModel?.order_amt else {
                return
            }
           
            payCount.text = "¥ " + String(describing: price) + "元"
           
            guard let time = orderDetailModel?.order_time else {
                return
            }

            let dateText = "订单日期 " + timeStampToString(String(describing: time))
            dateLabel.text = dateText
            
        }
    }
    
    
    fileprivate lazy var backView: UIView = UIView()
    fileprivate lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(r: 184, g: 184, b: 184)
        label.font = UIFont.systemFont(ofSize: 15*HEIGHT_SCALE)
        return label
    }()
    fileprivate lazy var line: UIView = {
        let line = UIView()
        line.backgroundColor = UIColor(r: 241, g: 241, b: 241)
        return line
    }()
    fileprivate lazy var serviceType: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 14*HEIGHT_SCALE)
        return label
    }()
    fileprivate lazy var typeImageView: UIImageView = UIImageView()
    fileprivate lazy var payCount: UILabel = {
        let label = UILabel()
        label.textColor = RGBA(0x00, g: 0xb8, b: 0xff, a: 1)
        label.font = UIFont.boldSystemFont(ofSize: 15*HEIGHT_SCALE)
        label.textAlignment = .right
        return label
    }()
    fileprivate lazy var arrowImage: UIImageView = {
        let arrow = UIImageView(imageName: "icon_xiayibu")
        return arrow
    }()
    fileprivate lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.textColor = RGBA(66, g: 66, b: 66, a: 1)
        label.font = UIFont.systemFont(ofSize: 12*HEIGHT_SCALE)
        return label
    }()
    fileprivate lazy var cateLabel: UILabel = {
        let label = UILabel()
        label.textColor = RGBA(0xf7, g: 0xf7, b: 0xf7, a: 1)
        return label
    }()
    
    fileprivate lazy var appointLabel: UILabel = {
        let label = UILabel()
        let attStr = NSMutableAttributedString(string: "[预约]", attributes: [NSForegroundColorAttributeName:UIColor.red])
        label.attributedText = attStr
        label.font = UIFont.systemFont(ofSize: 15*HEIGHT_SCALE)
//        label.isHidden = true
        return label
    }()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor(r: 249, g: 249, b: 249)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

extension OrderDetailCell {
    fileprivate func setupUI() {
        
        addSubview(backView)
        backView.backgroundColor = UIColor.white
        addSubview(nameLabel)
        addSubview(line)
        addSubview(serviceType)
        addSubview(typeImageView)
        addSubview(arrowImage)
        addSubview(payCount)
        addSubview(dateLabel)
        addSubview(cateLabel)
        addSubview(appointLabel)
        
        backView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView)
            make.size.equalTo(CGSize(width:kScreenW, height:90*HEIGHT_SCALE))
            make.left.equalTo(contentView)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(backView).offset(20*WIDTH_SCALE)
            make.size.equalTo(CGSize(width:100*WIDTH_SCALE, height:24*HEIGHT_SCALE))
            make.centerY.equalTo(backView.snp.top).offset(17*HEIGHT_SCALE)
        }
        
        line.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width:kScreenW,height:0.5))
            make.left.equalTo(backView)
            make.top.equalTo(backView.snp.top).offset(34*HEIGHT_SCALE)
        }
        
        serviceType.snp.makeConstraints { (make) in
            make.top.equalTo(line.snp.bottom).offset(12*HEIGHT_SCALE)
            make.left.equalTo(nameLabel)
            make.size.equalTo(CGSize(width:100, height:30))
        }
        
        typeImageView.snp.makeConstraints { (make) in
            make.left.equalTo(serviceType.snp.right).offset(9*WIDTH_SCALE)
            make.size.equalTo(CGSize(width:15*WIDTH_SCALE, height:15*WIDTH_SCALE))
            make.centerY.equalTo(serviceType.snp.centerY)
        }
        
        arrowImage.snp.makeConstraints { (make) in
            make.right.equalTo(backView).offset(-21*WIDTH_SCALE)
            make.top.equalTo(line).offset(21*HEIGHT_SCALE)
            make.size.equalTo(CGSize(width:9*WIDTH_SCALE, height:15*WIDTH_SCALE))
        }
        
        payCount.snp.makeConstraints { (make) in
            make.centerY.equalTo(arrowImage.snp.centerY)
            make.right.equalTo(arrowImage.snp.left).offset(-13*WIDTH_SCALE)
            make.size.equalTo(CGSize(width:100*WIDTH_SCALE, height:16*HEIGHT_SCALE))
        }
        
        dateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(backView.snp.bottom).offset(-5*HEIGHT_SCALE)
        }
        
        cateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(backView)
            make.top.equalTo(backView.snp.bottom)
            make.size.equalTo(CGSize(width:kScreenW, height:18*HEIGHT_SCALE))
        }
        
        appointLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right)
            make.bottom.equalTo(nameLabel)
        }
        
    }
}
